﻿using System;
using System.Linq;
using EzDesk.Core.Data;
using EzDesk.Core.Interface;
using EzDesk.Core.Model;
using System.Configuration;
using EzDesk.Utility.Extensions;
using EzDesk.Utility.Generic;

namespace EzDesk.Core.ServiceImpl.Services
{
    public partial class CoreClientService : ICoreClientService
    {
        private string _clientBaseUrl = null;

        #region [ ClientManagement Methods ]

        public string GetClientBaseUrl()
        {
            if (_clientBaseUrl == null)
            {
                _clientBaseUrl = geCurrenttClientBaseUrl();
            }
            return _clientBaseUrl;
        }

        private string geCurrenttClientBaseUrl()
        {
            EzDeskUserAgentType userAgentType = _operationProperties.UserAgent;
            var clientBaseUrl = "";
            switch (userAgentType)
            {
                case EzDeskUserAgentType.GenericMobile:
                    clientBaseUrl = ConfigurationManager.AppSettings["GenericMobileLocation"];
                    break;
                case EzDeskUserAgentType.DesktopClient:
                    clientBaseUrl = ConfigurationManager.AppSettings["DesktopClientLocation"];
                    break;
            }
            return clientBaseUrl;
        }

        #endregion


        public bool CheckOutOfPeriod()
        {
            return EzDeskDatabaseContainer.Go(
                container =>
                    {
                        var orgInfo = container.OrganizationSet.FirstOrDefault();
                        var expStr = orgInfo.Exp;
                        var dataStr = Des.DecryptDES(expStr, "weimargro");
                        var expDate = EpochDateTimeExtension.FromEpoch(Convert.ToInt64(dataStr.Substring(0, 10)));

                        return expDate < DateTime.Now.Date;
                    });

        }
    }
}